<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding-bottom: 800px;
        }

        table {
            border-collapse: collapse;
            border-spacing: 0;
            cursor: pointer;
        }

        table td {
            border: 1px solid black;
            padding: 4px 8px;
        }

        table thead {
            background-color: #dedede;
        }
        .menu{
            display: flex;
            flex-flow: column wrap;
            box-shadow: 0 0 6px #666;
            width: 100px;
            border-radius: 8px;
            position: fixed;
            top: 0px;
            left: 0px;
            background-color: white;
        }
        .menu span{
            height: 16px;
            line-height: 16px;
            font-size: 12px;
            padding: 8px 20px;
            cursor: pointer;
        }
        .menu span:hover{
            background-color: #dedede;
        }
        .map{
            position: fixed;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <td>id</td>
                    <td>name</td>
                    <td>username</td>
                    <td>age</td>
                    <td>tel</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for=" (u,i) in users " :key="u.id" v-menu:users="i">
                    <td>{{ u.id }}</td>
                    <td>{{ u.name }}</td>
                    <td>{{ u.username }}</td>
                    <td>{{ u.age }}</td>
                    <td>{{ u.tel }}</td>
                </tr>
            </tbody>
        </table>
        <hr>
        <table>
            <thead>
                <tr>
                    <td>id</td>
                    <td>name</td>
                    <td>username</td>
                    <td>age</td>
                    <td>tel</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for=" (u,i) in stus " :key="u.id" v-menu:stus="i">
                    <td>{{ u.id }}</td>
                    <td>{{ u.name }}</td>
                    <td>{{ u.username }}</td>
                    <td>{{ u.age }}</td>
                    <td>{{ u.tel }}</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script src="../assets/mock/mock-min.js"></script>
    <script type="module">
        import { createApp } from "../assets/vue3/vue.esm-browser.js";
        const app = createApp({
            data() {
                return {
                    users: Mock.mock({
                        "list|10": [
                            {
                                id: "@id",
                                name: "@cname",
                                username: "@word",
                                "age|20-80": 23,
                                tel: /^1\d{10}$/
                            }
                        ]
                    }).list,
                    stus: Mock.mock({
                        "list|10": [
                            {
                                id: "@id",
                                name: "@cname",
                                username: "@word",
                                "age|20-80": 23,
                                tel: /^1\d{10}$/
                            }
                        ]
                    }).list
                }
            }
        });

        let divBox = null;
        divBox = document.createElement("div");
        divBox.classList.add("menu");
        divBox.style.display = "none";

        let spanDetailDom = document.createElement("span");
        spanDetailDom.innerHTML = "详情";

        let spanSetDom = document.createElement("span");
        spanSetDom.innerHTML = "修改";

        let spanRemoveDom = document.createElement("span");
        spanRemoveDom.innerHTML = "删除";
        
        divBox.appendChild(spanDetailDom);
        divBox.appendChild(spanSetDom);
        divBox.appendChild(spanRemoveDom);

        document.body.appendChild(divBox);
        app.directive("menu", function (el, binding) {
            // vue3 新增属性 instance 表示实例对象
            // console.log(binding.instance); // 指令应用时所在的容器对应的vue实例
            // console.log(binding.value);
            // console.log(binding.instance.users); 
            // console.log(binding.arg)
            el.addEventListener("contextmenu", function (event) {
                divBox.style.display = null;
                divBox.style.top = event.y+"px";
                divBox.style.left = event.x+"px";
                spanRemoveDom.onclick = function(){
                    binding.instance[binding.arg].splice(binding.value,1)
                    divBox.style.display = "none";
                }
                event.preventDefault()
            })
            el.addEventListener("click",function(){
                divBox.style.display = "none";
            })
        })

        app.mount("#app")
    </script>
</body>

</html>